package cn.test;

/**
 * @ClassName Road
 * @Description TODO
 * @Author 郑万富
 * @Date 2020/11/9 10:35
 * @Version 1.0
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Road {
    //0:straight,1:left,2:right
    private List<Car> straightCars;
    private List<Car> leftCars;
    private List<Car> rightCars;
    //0:N,1:E,2:S,3:W
    private int roadNum;
    private static Random random;
    static {
        random = new Random();
    }
    //在这条路上随机添加一辆任意方向的车
    public void addRandomCar(){
        int forwardNum = random.nextInt(3);
        Car car = new Car(roadNum,forwardNum);
        switch (forwardNum){
            case 0:
                straightCars.add(car);
                break;
            case 1:
                leftCars.add(car);
                break;
            case 2:
                rightCars.add(car);
                break;
        }
    }
    //初始化时，给该条路上添加5辆随机方向的车
    public Road(int roadNum) {
        straightCars = new ArrayList<>();
        leftCars = new ArrayList<>();
        rightCars = new ArrayList<>();
        this.roadNum = roadNum;
        for(int i=0;i<5;i++){
            addRandomCar();
        }
    }
    //在这条路上根据要前进的方向移除队首的车辆，模拟车辆移动，
    //0:straight,1:left,2:right
    public void removeOneCarByForward(int forward){
        switch (forward){
            case 0:
                if(straightCars.size()!=0) {
                    straightCars.remove(0);
                }
                break;
            case 1:
                if(leftCars.size()!=0) {
                    leftCars.remove(0);
                }
                break;
            case 2:
                if(rightCars.size()!=0) {
                    rightCars.remove(0);
                }
                break;
        }
    }

    //获取N(0)，S(2)两条路的车辆状态字符串
    public String getCarLineVertical(int n){
        switch (roadNum){
            case 0:
                return getCarString(rightCars,n)+" "+getCarString(straightCars,n)+" "+getCarString(leftCars,n);
            case 2:
                return getCarString(leftCars,n)+" "+getCarString(straightCars,n)+" "+getCarString(rightCars,n);
        }
        return " ";
    }
    //获取W(3)，E(1)两条路的车辆状态字符串
    public StringBuilder[] getCarLineHorizontal(){
        StringBuilder lines[] = new StringBuilder[3];
        for(int i=0;i<3;i++){
            lines[i] = new StringBuilder("");
        }
        switch (roadNum){
            case 1:
                lines[0].append(getCarString(rightCars,0)).append(" ")
                        .append(getCarString(rightCars,1)).append(" ")
                        .append(getCarString(rightCars,2)).append(" ")
                        .append(getCarString(rightCars,3)).append(" ")
                        .append(getCarString(rightCars,4)).append(" ");
                lines[1].append(getCarString(straightCars,0)).append(" ")
                        .append(getCarString(straightCars,1)).append(" ")
                        .append(getCarString(straightCars,2)).append(" ")
                        .append(getCarString(straightCars,3)).append(" ")
                        .append(getCarString(straightCars,4)).append(" ");
                lines[2].append(getCarString(leftCars,0)).append(" ")
                        .append(getCarString(leftCars,1)).append(" ")
                        .append(getCarString(leftCars,2)).append(" ")
                        .append(getCarString(leftCars,3)).append(" ")
                        .append(getCarString(leftCars,4)).append(" ");
                break;
            case 3:
                lines[0].insert(0,getCarString(leftCars,0)).insert(0," ")
                        .insert(0,getCarString(leftCars,1)).insert(0," ")
                        .insert(0,getCarString(leftCars,2)).insert(0," ")
                        .insert(0,getCarString(leftCars,3)).insert(0," ")
                        .insert(0,getCarString(leftCars,4)).insert(0," ");
                lines[1].insert(0,getCarString(straightCars,0)).insert(0," ")
                        .insert(0,getCarString(straightCars,1)).insert(0," ")
                        .insert(0,getCarString(straightCars,2)).insert(0," ")
                        .insert(0,getCarString(straightCars,3)).insert(0," ")
                        .insert(0,getCarString(straightCars,4)).insert(0," ");
                lines[2].insert(0,getCarString(rightCars,0)).insert(0," ")
                        .insert(0,getCarString(rightCars,1)).insert(0," ")
                        .insert(0,getCarString(rightCars,2)).insert(0," ")
                        .insert(0,getCarString(rightCars,3)).insert(0," ")
                        .insert(0,getCarString(rightCars,4)).insert(0," ");
                break;
        }
        return lines;
    }

    //如果list[index]位置上没有元素，则返回空格字符串
    private String getCarString(List<Car> list, int n){
        if(n>=list.size()){
            return "   ";
        }else{
            return list.get(n).getForward();
        }
    }

}
